package com.jt.auth.service;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * UserDetailService接口的实现类负责：
 *   1.通过查询内存获取用户信息（默认）
 *   2.通过数据库获取用户信息
 *   3.通过Feign接口调用远端service获取用户信息（本次选择）
 * */
@Service
public class UserDetailServiceImpl implements UserDetailsService {

    @Autowired
    private RemoteUserService remoteUserService;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        com.jt.auth.pojo.User user = remoteUserService.selectUserByUsername(username);
        if (user == null)
            throw new UsernameNotFoundException("user is not exist");
        List<String> permissions = remoteUserService.selectUserPermissions(user.getId());

        User userInfo = new User(username,
                user.getPassword(),
                AuthorityUtils.createAuthorityList(permissions.toArray(new String[]{})));
        return userInfo;
    }

    /**
     * AuthenticationManagement  认证管理器
     * */
}
